home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / sockets.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  13.3 KB  |  612 lines

  1. /* This has the socket code, [dis]connecting server code, etc. */
  2. /* ----------------------------------------------------------- */
  3.  
  4. #include "headers.h" /* has all important stuff */
  5.  
  6. /* called at first of main() to connect to info server */
  7. void initConn(char *primserv, char *secserv)
  8. {
  9. #  if !defined(SUN) && !defined(BSD)
  10.    int res;
  11. #  endif
  12.  
  13.    unsigned long inaddr;
  14.    char readbuf[MAXREADSIZE];
  15.  
  16.    struct hostent *hent = NULL;
  17.  
  18.    prim = 1;
  19.  
  20.    if (silent != 1) debug("now attempting to connect to an info server...\n");
  21.    
  22. Connect:
  23.    errno = 0, connected = 0;
  24.  
  25.    memset(readbuf, 0, sizeof(readbuf));
  26.    memset(&daddr.sin_zero, 0, sizeof(daddr.sin_zero));
  27.  
  28.    inaddr = (unsigned long int)inet_addr(((prim) ? primserv : secserv));
  29.  
  30.    if (inaddr != INADDR_NONE)
  31.       memcpy(&daddr.sin_addr, &inaddr, sizeof(struct in_addr));   
  32.  
  33.    else
  34.    {
  35.       hent = gethostbyname(((prim == 1) ? primserv : secserv));
  36.  
  37.       if (hent == NULL)
  38.       {
  39.          if (silent != 1)
  40.          {
  41.             if (h_errno == HOST_NOT_FOUND)
  42.                error("error with gethostbyname: host not found\n");
  43.  
  44.             else if (h_errno == TRY_AGAIN)
  45.                error("error with gethostbyname: "
  46.                      "non-auth'd host/server failure\n");
  47.  
  48.             else if (h_errno == NO_RECOVERY)
  49.                error("error with gethostbyname: non-recoverable error\n");
  50.  
  51.             else if (h_errno == NO_DATA)
  52.                error("error with gethostbyname: no data request for this\n");
  53.  
  54.             else if (h_errno == NO_ADDRESS)
  55.                error("error with gethostbyname: no address\n");
  56.  
  57.             if (debugging == 1)
  58.             {
  59.                (void)putchar('\n');
  60.                (void)write(dblogfd, "\n", 1);
  61.             }
  62.  
  63.             (void)write(errlogfd, "\n", 1);
  64.          }
  65.  
  66.          if (prim == 1)
  67.          {
  68.             prim = 0; /* use secondary server */
  69.             goto Connect;
  70.          }
  71.  
  72.          else /* they might not have a DNS capabilities.. bad. */
  73.          {
  74.             error("both primary/secondary dns resolutions failed.."
  75.                   "aborting\n\n");
  76.  
  77.             quit(ERROR);
  78.          }
  79.       }
  80.  
  81.       memcpy((char *)&daddr.sin_addr, hent->h_addr, hent->h_length);
  82.    }
  83.  
  84.    if (sockfd > 0)
  85.    {
  86.      (void)close(sockfd);
  87.      sockfd = 0;
  88.    }
  89.  
  90.    sockfd = socket(AF_INET, SOCK_STREAM, 0);
  91.    if ((sockfd == ERROR) && (errno > 0))
  92.    {
  93.       if (spooling == 1) 
  94.          error("(in spool parent) error with socket(): %s\n\n",
  95.                strerror(errno));
  96.  
  97.       else error("error with socket(): %s\n\n", strerror(errno));
  98.  
  99.      quit(ERROR);
  100.    }
  101.  
  102.    setsockopts(sockfd, 1);
  103.  
  104.    if (locPort <= 0) locPort = LOCPORT;
  105.  
  106.    laddr.sin_addr.s_addr = INADDR_ANY;
  107.    laddr.sin_family = daddr.sin_family = AF_INET;
  108.    laddr.sin_port = htons(locPort), daddr.sin_port = htons(PORT);
  109.    memset(&(laddr.sin_zero), 0, 8), memset(&(daddr.sin_zero), 0, 8);
  110.  
  111. # if !defined(SUN) && !defined(BSD)
  112. #  ifdef _POSIX_SAVED_IDS
  113.    res = setuid(0);
  114. #  else
  115.    res = seteuid(0);
  116. #  endif
  117.  
  118.    if (res == ERROR)
  119.    {
  120.       error("error setting [e]uid: %s\n\n", strerror(errno));
  121.       quit(ERROR);
  122.    }
  123. # endif
  124.  
  125.    rbindport();
  126.  
  127. # if !defined(SUN) && !defined(BSD)
  128.    if (pwd != NULL)
  129.    {
  130. #     ifdef _POSIX_SAVED_IDS
  131.       res = setuid(pwd->pw_uid);
  132. #     else
  133.       res = seteuid(pwd->pw_uid);
  134. #     endif
  135.  
  136.       if (res == ERROR)
  137.       {
  138.          error("error setting [e]uid: %s\n\n", strerror(errno));
  139.          quit(ERROR);
  140.       }
  141.    }
  142. # endif
  143.  
  144.    if (hent && hent->h_name)  
  145.    {
  146.       if (silent != 1)
  147.       {
  148.          if (debugging == 1) 
  149.          {
  150.             (void)putchar('\n');
  151.             (void)write(dblogfd, "\n", 1);
  152.          }
  153.  
  154.          (void)printf("%connecting to %s (%s) [%s info server]...\n",
  155.                       (debugging != 1 ? 'C' : 'c'),
  156.                       (prim == 1 ? primserv : secserv), 
  157.                       (char *)inet_ntoa(daddr.sin_addr),
  158.                       (prim == 1 ? "primary" : "secondary"));
  159.       }
  160.    }
  161.  
  162.    else 
  163.    {
  164.       if (silent != 1)
  165.       {
  166.          if (debugging == 1) 
  167.          {
  168.             (void)putchar('\n');
  169.             (void)write(dblogfd, "\n", 1);
  170.          }
  171.  
  172.          (void)printf("%connecting to %s [%s info server]...\n",
  173.                       (debugging != 1 ? 'C' : 'c'), 
  174.                       (char *)inet_ntoa(daddr.sin_addr),
  175.                       (prim == 1 ? "primary" : "secondary"));
  176.       }
  177.    }
  178.  
  179.    while(1)
  180.    {
  181.       errno = 0, connected = 0;
  182.  
  183.       if (connect(sockfd, (struct sockaddr *)&daddr,
  184.                   sizeof(struct sockaddr)) == ERROR)
  185.       {
  186.          if (errno == EINTR) continue;
  187.  
  188.          if (silent != 1)
  189.          {
  190.             if (debugging == 1)
  191.             {
  192.                (void)putchar('\n');
  193.                (void)write(dblogfd, "\n", 1);
  194.             }
  195.  
  196.             error("error with connect(): %s\n"
  197.                   "unable to connect to the %s server\n\n", strerror(errno),
  198.                   (prim == 1 ? "primary" : "secondary"));
  199.          }
  200.    
  201.          if (prim)
  202.          {
  203.             if (silent != 1) 
  204.                error("now connecting to secondary server..\n\n");
  205.  
  206.             prim = 0; /* use secondary server */
  207.             goto Connect;
  208.          }
  209.  
  210.          else 
  211.          {
  212.             /* couldn't connect to either server.. start spooling */
  213.             if (silent != 1) debug("had some errors with both servers..\n");
  214.             errors = 1; 
  215.  
  216.             return;
  217.          }
  218.       }
  219.  
  220.       else
  221.       {
  222.          debug("connect() succeeded...\n\n");
  223.          break;
  224.       }
  225.    }
  226.  
  227. #ifndef NOSSL
  228. # if !defined(SUN) && !defined(BSD)
  229. #  ifdef _POSIX_SAVED_IDS
  230.    res = setuid(0);
  231. #  else
  232.    res = seteuid(0);
  233. #  endif
  234.  
  235.    if (res == ERROR)
  236.    {
  237.       error("error setting [e]uid: %s\n\n", strerror(errno));
  238.       quit(ERROR);
  239.    }
  240. # endif
  241. #endif
  242.  
  243.    makeSSLconn();
  244.  
  245. # if !defined(SUN) && !defined(BSD)
  246.    if (pwd != NULL)
  247.    {
  248. #     ifdef _POSIX_SAVED_IDS
  249.       res = setuid(pwd->pw_uid);
  250. #     else
  251.       res = seteuid(pwd->pw_uid);
  252. #     endif
  253.  
  254.       if (res == ERROR)
  255.       {
  256.          error("error setting [e]uid: %s\n\n", strerror(errno));
  257.          quit(ERROR);
  258.       }
  259.    }
  260. # endif
  261.  
  262.    recv_data(readbuf, sizeof(readbuf));
  263.    if (silent != 1)
  264.    {
  265.       if (debugging == 1)
  266.       {
  267.          (void)putchar('\n');
  268.          (void)write(dblogfd, "\n", 1);
  269.       }
  270.  
  271.       debug("checking for successful connect\n");
  272.       debug("(the data is): %s%c", readbuf, 
  273.             (strchr(readbuf, '\n') == NULL ? '\n' : '\0'));
  274.    }
  275.  
  276.    if (strncmp(readbuf, "SUCCESSFUL connect", 18) == 0)
  277.    {
  278.       connected = 1;
  279.  
  280.       if (silent == 1)
  281.       {
  282.          silent = 0;
  283.  
  284.          if (spooling == 1)
  285.             (void)printf("(in spool parent) "
  286.                          "now connected to an info server\n");
  287.       }
  288.  
  289.       else 
  290.          (void)printf("%cow connected\n\n", (debugging == 1 ? 'n' : 'N'));
  291.  
  292.       return;
  293.    }
  294.  
  295.    else
  296.    {
  297.       connected = 0;   
  298.       error("ERROR connecting to server\n"
  299.             "error message: %s\n%c", readbuf, 
  300.             (strchr(readbuf, '\n') == NULL ? '\n' : '\0'));
  301.  
  302.       if (prim == 1)
  303.       {
  304.          prim = 0;
  305.          goto Connect;
  306.       }
  307.  
  308.       else
  309.       {
  310.          if (silent != 1) debug("had some errors with both servers..\n");
  311.  
  312.          errors = 1;
  313.          return;
  314.       }
  315.    }
  316. }
  317.       
  318.       
  319. /* --------------------- */
  320.       
  321.       
  322. /* connect to a server in the list.. not an info server */
  323. int connServ(char *host)
  324. {     
  325. #  if !defined(BSD) && !defined(SUN)
  326.    int res;
  327. #  endif
  328.  
  329.    char readbuf[MAXREADSIZE];
  330.    struct hostent *hent = NULL;
  331.  
  332.    memset(readbuf, 0, sizeof(readbuf));
  333.    
  334.    hent = gethostbyname(host);
  335.    if ((hent == NULL) && (silent != 1))
  336.    {
  337.       if (h_errno == HOST_NOT_FOUND)
  338.          error("error with gethostbyname: host not found\n\n");
  339.  
  340.       else if (h_errno == TRY_AGAIN)
  341.          error("error with gethostbyname: "
  342.                "non-auth'd host/server failure\n\n");
  343.  
  344.       else if (h_errno == NO_RECOVERY)
  345.          error("error with gethostbyname: non-recoverable error\n\n");
  346.  
  347.       else if (h_errno == NO_DATA)
  348.          error("error with gethostbyname: no data request for this\n\n");
  349.  
  350.       else if (h_errno == NO_ADDRESS)
  351.          error("error with gethostbyname: no address\n\n");
  352.  
  353.       if (debugging == 1)
  354.       {
  355.          (void)putchar('\n');
  356.          (void)write(dblogfd, "\n", 1);
  357.       }
  358.  
  359.       return ERROR;      
  360.    }
  361.  
  362.    if (sockfd > 0)
  363.    {
  364.       (void)close(sockfd);
  365.       sockfd = 0;
  366.    }
  367.  
  368.    sockfd = socket(AF_INET, SOCK_STREAM, 0);
  369.    if ((sockfd == ERROR) && (errno > 0))
  370.    {
  371.       if (silent == 1)
  372.          error("(in spool parent) error with socket(): %s\n\n", 
  373.                strerror(errno));
  374.  
  375.       else error("error with socket(): %s\n\n", strerror(errno));
  376.  
  377.       quit(ERROR);  
  378.    }  
  379.       
  380.    setsockopts(sockfd, 1);
  381.  
  382.    if (locPort <= 0) locPort = LOCPORT;
  383.  
  384.    laddr.sin_family = AF_INET;
  385.    laddr.sin_port = htons(locPort);
  386.    laddr.sin_addr.s_addr = INADDR_ANY;
  387.  
  388.    if (hent == NULL) 
  389.    {
  390.       /* we're fucked.. so just spool locally */
  391.       error("unable to resolve server's hostname.. now spooling\n\n");
  392.       doSpooling();
  393.    }
  394.  
  395.    daddr.sin_family = AF_INET;
  396.    daddr.sin_port = htons(servList[curServ].port);
  397.    daddr.sin_addr = *((struct in_addr *)hent->h_addr);
  398.  
  399.    memset(&(laddr.sin_zero), 0, 8), memset(&(daddr.sin_zero), 0, 8);
  400.  
  401. # if !defined(SUN) && !defined(BSD)
  402. #  ifdef _POSIX_SAVED_IDS
  403.    res = setuid(0);
  404. #  else
  405.    res = seteuid(0);
  406. #  endif
  407.  
  408.    if (res == ERROR)
  409.    {
  410.       error("error setting [e]uid: %s\n\n", strerror(errno));
  411.       quit(ERROR);
  412.    }
  413. # endif
  414.  
  415.    rbindport();
  416.  
  417. # if !defined(SUN) && !defined(BSD)
  418.    if (pwd != NULL)
  419.    {
  420. #     ifdef _POSIX_SAVED_IDS
  421.       res = setuid(pwd->pw_uid);
  422. #     else
  423.       res = seteuid(pwd->pw_uid);
  424. #     endif
  425.  
  426.       if (res == ERROR)
  427.       {
  428.          error("error setting [e]uid: %s\n\n", strerror(errno));
  429.          quit(ERROR);
  430.       }
  431.    }
  432. # endif
  433.  
  434.    if (silent != 1)
  435.    {
  436.       if (debugging == 1) 
  437.       {
  438.          (void)putchar('\n');
  439.          (void)write(dblogfd, "\n", 1);
  440.       }
  441.  
  442.       if (strstr(host, (char *)inet_ntoa(daddr.sin_addr)) != NULL)
  443.          (void)printf("%connecting to %s...\n", 
  444.                       (debugging == 1 ? 'c' : 'C'), host);      
  445.  
  446.       else
  447.          (void)printf("%connecting to %s (%s)...\n", 
  448.                       (debugging == 1 ? 'c' : 'C'),
  449.                       host, (char *)inet_ntoa(daddr.sin_addr));
  450.    }
  451.  
  452.    while(1)
  453.    {
  454.       if (connect(sockfd, (struct sockaddr *)&daddr,
  455.                   sizeof(struct sockaddr)) == ERROR)
  456.       {
  457.          if (errno == EINTR) continue;
  458.  
  459.          if (silent != 1)
  460.          {
  461.             if (debugging == 1)
  462.             {
  463.                (void)putchar('\n');
  464.                (void)write(dblogfd, "\n", 1);
  465.             }
  466.  
  467.             error("error connecting: %s\n\n", strerror(errno));
  468.          }
  469.  
  470.          return ERROR;   
  471.       }
  472.  
  473.       else
  474.       {
  475.          debug("connect() succeeded...\n\n");
  476.          break;
  477.       }
  478.    }
  479.  
  480. #ifndef NOSSL
  481. # if !defined(SUN) && !defined(BSD)
  482. #  ifdef _POSIX_SAVED_IDS
  483.    res = setuid(0);
  484. #  else
  485.    res = seteuid(0);
  486. #  endif
  487.  
  488.    if (res == ERROR)
  489.    {
  490.       error("error setting [e]uid: %s\n\n", strerror(errno));
  491.       quit(ERROR);
  492.    }
  493. # endif
  494.  
  495.    makeSSLconn();
  496.  
  497. # if !defined(SUN) && !defined(BSD)
  498.    if (pwd != NULL)
  499.    {
  500. #     ifdef _POSIX_SAVED_IDS
  501.       res = setuid(pwd->pw_uid);
  502. #     else
  503.       res = seteuid(pwd->pw_uid);
  504. #     endif
  505.  
  506.       if (res == ERROR)
  507.       {
  508.          error("error setting [e]uid: %s\n\n", strerror(errno)); 
  509.          quit(ERROR);
  510.       }
  511.    }
  512. # endif
  513. #endif
  514.  
  515.    recv_data(readbuf, sizeof(readbuf));
  516.    if (silent != 1) debug("checking for successful connect\n");
  517.    
  518.    if (strncmp(readbuf, "SUCCESSFUL connect", 18) == 0)
  519.    {
  520.       connected = 1;
  521.  
  522.       if ((silent == 1) && (spooling == 1))
  523.       {
  524.          silent = 0;
  525.          (void)printf("(in spool parent) now connected to %s\n\n", host);
  526.       }
  527.  
  528.       else (void)printf("%cow connected\n\n", (debugging == 1 ? 'n' : 'N'));
  529.  
  530.       return 0;
  531.    }
  532.  
  533.    else
  534.    {
  535.       connected = 0;   
  536.  
  537.       if (silent == 1)
  538.          error("(in spool parent) ERROR received from server\n"
  539.                "error message: %s\n%c", readbuf,
  540.                (strchr(readbuf, '\n') == NULL ? '\n' : '\0'));
  541.      
  542.       else
  543.          error("ERROR received from server\nerror message: %s\n%c",
  544.                readbuf, (strchr(readbuf, '\n') == NULL ? '\n' : '\0'));
  545.  
  546.       return ERROR;
  547.    }
  548. }
  549.  
  550.  
  551. /* ------------------------------ */
  552.  
  553.  
  554. /* set socket options */
  555. void setsockopts(int fd, int all)
  556. {
  557.    int res;
  558.    int val = 1;
  559.  
  560.    if (all == 1)
  561.    {
  562. #     ifdef SO_KEEPALIVE
  563.       /* val = 1; */
  564.       res = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&val,
  565.                        sizeof(val));
  566.  
  567.       if (res == ERROR)
  568.       {
  569.          error("error with setsockopt (SO_KEEPALIVE): %s\n\n", 
  570.                strerror(errno));
  571.  
  572.          quit(ERROR);
  573.       }
  574.  
  575. #     ifdef TCP_KEEPALIVE
  576.       val = PROBETIME;
  577.  
  578.       res = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, (char *)&val,
  579.                        sizeof(val));
  580.  
  581.       if (res == ERROR)
  582.       {
  583.          error("error with setsockopt (TCP_KEEPALIVE): %s\n\n", 
  584.                strerror(errno));
  585.  
  586.          quit(ERROR);
  587.       }
  588.  
  589. #     endif
  590. #     endif
  591.  
  592.       return;
  593.    }
  594.  
  595.    else
  596.    {
  597.       val = 1;
  598.       res = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, 
  599.                        sizeof(val));
  600.  
  601.       if (res == ERROR)
  602.       {
  603.          error("error with setsockopt (SO_REUSEADDR): %s\n\n",
  604.                strerror(errno));
  605.  
  606.          quit(ERROR);
  607.       }
  608.    }
  609.  
  610.    return;
  611. }
  612.